{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品轮播图</title>
    <style>
        .goods_type {
            width: 100%;
            max-width: 560px;
            margin-left: 0; /* 左对齐 */
        }

        .main-image {
            position: relative;
            margin-bottom: 20px;
            width: 100%;
            height: 546px;
            overflow: hidden;
            border-radius: 8px;
        }

        .main-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 8px;
            cursor: pointer;
        }

        .thumbnails {
            display: flex;
            justify-content: flex-start;
            flex-wrap: wrap;
            gap: 10px;
            padding: 0;
        }

        .thumbnail {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 4px;
            border: 2px solid transparent;
            cursor: pointer;
            transition: border 0.3s ease;
            background-color: #f0f0f0;
        }

        .thumbnail img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .thumbnail.active {
            border: 2px solid #000;
        }

        .thumbnail:hover:not(.active) {
            border: 2px solid #ccc;
        }
    </style>
</head>
<body>
<div class="goods_type">
    <div class="goods_box">
        <div class="box_pic main-image">
            <img src="{% static goods_obj.image_set.first.img_address.name %}" alt="商品主图" id="mainImage">
        </div>
        <div class="thumbnails">
            {% for image in goods_obj.image_set.all %}
                <div class="thumbnail" data-index="{{ forloop.counter0 }}">
                    <img src="{% static image.img_address.name %}" alt="Thumbnail">
                </div>
            {% endfor %}
        </div>
    </div>
</div>

</body>

<script>
        document.addEventListener('DOMContentLoaded', () => {
            const mainImage = document.getElementById('mainImage');
            const thumbnails = document.querySelectorAll('.thumbnail');

            thumbnails.forEach(thumbnail => {
                thumbnail.addEventListener('click', () => {
                    const imgSrc = thumbnail.querySelector('img').src;
                    mainImage.src = imgSrc;
                    thumbnails.forEach(t => t.classList.remove('active'));
                    thumbnail.classList.add('active');
                });
            });

            // 默认选中第一张缩略图
            if (thumbnails.length > 0) {
                thumbnails[0].classList.add('active');
            }
        });
    </script>
</html>